首页>代码>java Swing开发简单备忘录源代码下载>/no forget/src/com/java/util/MyDbUtil.java
package com.java.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class MyDbUtil {
	public Connection conn = null;

	public Connection getConnection() {
		String username = "root";
		String password = "123456";
		String url = "jdbc:mysql://localhost:3308/no_forget";

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, username,
					password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return conn;
	}

	public void closeConn(Connection conn, Statement stm, ResultSet rs) {
		if (rs != null)
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (stm != null)
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public Vector<Vector> getAllThings(){
		Vector <Vector>things=new Vector();
		Connection conn=null;
		Statement stm=null;
		ResultSet rs=null;
		conn=new MyDbUtil().getConnection();
		try {
			stm=conn.createStatement();
			rs=stm.executeQuery("select * from things");
			int i=1;   //行号
			while(rs.next()){
				Vector<String> v=new Vector<>();
				v.add(""+i++);
				v.add("NF"+rs.getInt("num")+"");
				v.add(rs.getString("title"));
				v.add(rs.getString("content"));
				v.add(rs.getString("time"));
				things.add(v);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return things;
	}
	
	public void addThing(int num,String title,String content,String time){
		Connection conn=null;
		PreparedStatement pstm=null;
		conn=new MyDbUtil().getConnection();
		try {
			if(num<0){
			pstm=conn.prepareStatement("insert into things values(default,?,?,?);");
			pstm.setString(1, title);
			pstm.setString(2, content);
			pstm.setString(3, time);
			}
			else{
				pstm=conn.prepareStatement("update things set title=?,"
						+ "content=?,time=? where num=?;");
				pstm.setString(1, title);
				pstm.setString(2, content);
				pstm.setString(3, time);
				pstm.setInt(4, num);
			}
			pstm.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void deleteThing(int num){
		Connection conn=null;
		PreparedStatement pstm=null;
		conn=new MyDbUtil().getConnection();
		try {
				pstm=conn.prepareStatement("delete from things where"
						+ " num="+num);
			pstm.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new MyDbUtil().addThing(-1,"title3", "内容", "测试的・");
		Vector<Vector> vv=new MyDbUtil().getAllThings();
		for (int i = 0; i < vv.size(); i++) {
			Vector<String> v=vv.get(i);
			for (int j = 0; j < v.size(); j++) {
				System.out.print(v.get(j)+"   ");
			}
			System.out.println();
		}
	}
}
最近下载更多
李朝磊  LV18 2023年11月18日
weilaizhisheng  LV21 2023年9月28日
ezra77934  LV2 2023年9月15日
alexcheung  LV3 2023年7月10日
微信网友_6292282163859456  LV4 2023年1月5日
计算机暴龙战士  LV19 2022年12月6日
a318888331  LV13 2022年10月31日
alonely23  LV1 2022年10月6日
zhendong  LV7 2022年9月26日
xiao小果  LV12 2022年6月22日
最近浏览更多
3334004690  LV3 3月6日
shiaomon 2023年12月14日
暂无贡献等级
Anzhui  LV2 2023年12月10日
李朝磊  LV18 2023年11月18日
tianshi  LV7 2023年10月19日
weilaizhisheng  LV21 2023年9月28日
ezra77934  LV2 2023年9月15日
alexcheung  LV3 2023年7月10日
ds9009  LV7 2023年6月26日
1145304128  LV12 2023年5月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友